<?php
/**
 * 
 * @author lunzi
 * @date 15-06-15
 * @uses 授权
 *
 */
class Oauth extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->config('wx');
	}
	
	//获取code
	public function index()
	{
		$request_url = $this->input->get('url');
		$wx = $this->config->item('wx');
		//获取code
		$appid = $wx['appid'];
 		$redirect_uri = 'http%3a%2f%2f'.$_SERVER['HTTP_HOST'].'/wap/oauth/get_access';
		$response_type = 'code';
		$scope = 'snsapi_userinfo';
		$state = $request_url;
		$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
		
		header('location:'.$url);
	}
	
	public function get_access()
	{
		$wx = $this->config->item('wx');
		$this->load->library('curl');
		$this->load->library('wx_visitor');
		
		//判断是否获取code成功
		$this->input->get('code') ? $code = $this->input->get('code') : exit(json_encode(array('status'=>false,'data'=>'授权失败！')));
		$request_url = $this->input->get('state');
		//获取token和openID
		$appid = $wx['appid'];
		$secret = $wx['appsecret'];
		$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
		
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$get_token_url);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		$res_token = curl_exec($ch);
		curl_close($ch);
		
		//$res_token = $this->curl->simple_post($get_token_url);
		$token_obj = json_decode($res_token,true);
		if(!isset($token_obj['openid'])){//判断是否获取token成功
			exit(json_encode(array('status'=>false,'data'=>'授权失败！')));
		}
		
		//根据openid和access_token查询用户信息
		$access_token = $token_obj['access_token'];
		$openid = $token_obj['openid'];
		$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
		
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$get_user_info_url);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		$res_user = curl_exec($ch);
		curl_close($ch);
		
		//$res_user = $this->curl->simple_post($get_user_info_url);
		$user_obj = json_decode($res_user,true);
		if(!isset($user_obj['openid'])){//判断是否获取用户信息成功
			exit(json_encode(array('status'=>false,'data'=>'授权失败！')));
		}

		//根据open_id获取用户信息，看是否为会员,如果存在则更新数据,否则添加用户
		$this->load->model('user_model');
		$user = $this->user_model->checkUser(array('open_id'=>$user_obj['openid']));
		if($user){
			$user_data = $user;
			$user_data['open_id'] = $user_obj['openid'];
			$user_data['user_nick'] = $user_obj['nickname'];
			$user_data['avatar'] = $user_obj['headimgurl'];
			$user_data['access_token'] = $token_obj['access_token'];
			$user_data['refresh_token'] = $token_obj['refresh_token'];
			$this->user_model->editUser($user,$user_data);
		}
		else{
			$user_add['open_id'] = $user_obj['openid'];
			$user_add['user_nick'] = $user_obj['nickname'];
			$user_add['avatar'] = $user_obj['headimgurl'];
			$user_add['access_token'] = $token_obj['access_token'];
			$user_add['refresh_token'] = $token_obj['refresh_token'];
			$user_id = $this->user_model->addUser($user_add);
			$user_data = $this->user_model->checkUser(array('id'=>$user_id));
		}
		$this->wx_visitor->assign($user_data);
		redirect('http://'.$_SERVER['HTTP_HOST'].$request_url);
	}
	
	//获取接口调用凭据token
	private function api_token($APPID,$APPSECRET)
	{
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$APPID.'&secret='.$APPSECRET;
	
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		$api_token = curl_exec($ch);
		curl_close($ch);
		$api_token = json_decode($api_token,true);
		return $api_token['access_token'];
	}
}